using System;
using System.Text;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using Framework;

namespace SchemaMonitor
{
    //Table-Mapping Class (Customisable half)
    public partial class CTag
    {
        #region Constants
        public const string JOIN_TYPE = TABLE_NAME + " INNER JOIN " + CType.TABLE_NAME + " ON TypeId=TagForTypeId";
        #endregion

        #region Constructors (Public)
        //Default Connection String
        public CTag() : base() {}

        //Alternative Connection String
        public CTag(CDataSrc dataSrc) : base(dataSrc) {}
        
        //Hidden  (UI code should use cache instead)
        protected internal CTag(int tagId) : base(tagId) {}
        protected internal CTag(CDataSrc dataSrc, int tagId) : base(dataSrc, tagId) {}
        protected internal CTag(CDataSrc dataSrc, int tagId, IDbTransaction txOrNull) : base(dataSrc, tagId, txOrNull) { }
        #endregion

        #region Default Values
        protected override void InitValues_Custom()
        {
            //_sampleDateCreated = DateTime.Now;
            //_sampleSortOrder   = 0;
        }
        #endregion
        
        #region Default Connection String
        protected override CDataSrc DefaultDataSrc()  { return CDataSrc.Default;  }
        #endregion

        #region Properties - Relationships
        //Relationships - Foriegn Keys (e.g parent)
        public CType Parent    { get { return CType.Cache.GetById(this.TagForTypeId); } }
        public CType FieldType { get { return CType.Cache.GetById(this.TagIsTypeId);  } }

        //Relationships - Collections (e.g. children)
        #endregion

        #region Properties - Customisation

        //Derived/ReadOnly (e.g. xml classes, presentation logic)
        public string Path
        {
            get
            {
                var t = Parent;
                if (t.IsSystem)
                    return "System." + TagName;
                if (t.IsManual)
                    return string.Concat("[", t.TypeName, "].", TagName);
                var parentTags = CTag.Cache.GetByIsTypeId(t.TypeId);
                if (parentTags.Count == 0)
                    return string.Concat("[", t.TypeName, "].", TagName);

                var p = parentTags[0];
                if (parentTags.Count > 1)
                    foreach (var i in parentTags)
                        if (i.Parent.IsManual)
                            p = i;
                return string.Concat(p.Path, ".", TagName);
            }
        }
        #endregion

        #region Save/Delete Overrides
        //Can Override base.Save/Delete (e.g. Cascade deletes, or insert related records)
        #endregion

        #region Custom Database Queries
        //(Not normally required for cached classes, use list class for searching etc)
        //For Stored Procs can use: MakeList (matching schema), or DataSrc.ExecuteDataset (reports etc)
        //For Dynamic sql, can use: SelectSum, SelectDistinct, SelectCount, SelectWhere (inherited methods)
        internal CTagList SelectInScope()
        {
            return SelectWhere(CType.InScope(), JOIN_TYPE);
        }
        #endregion

        #region Searching (Optional)
        //For cached classes, custom seach logic resides in static methods on the list class
        // e.g. CTag.Cache.Search("...")

        //See also the auto-generated methods based on indexes
        //' e.g. CTag.Cache.GetBy...
        #endregion

        #region Caching Details
        //Cache Key
        internal static string CACHE_KEY = typeof(CTag).ToString();    //TABLE_NAME

        //Cache data
        private static CTagList LoadCache() { return new CTag().SelectInScope(); }

        //Cache Timeout
        private static void SetCache(CTagList value)
        {
            if (null != value)  
                value.Sort(); 
            CCache.Set(CACHE_KEY, value);    //Optional parameter can override timeout (otherwise uses config-settings, which default to 3hrs)
        }
        //Helper Method
        private CTag CacheGetById(CTagList list)  { return list.GetById(this.TagId);    }
        #endregion

        #region Cloning
        public CTag Clone(CDataSrc target, IDbTransaction txOrNull) //, int parentId)
        {
            //Shallow copy: Copies the immediate record, excluding autogenerated Pks
            CTag copy = new CTag(this, target);

            //Deep Copy - Child Entities: Cloned children must reference their cloned parent
            //copy.SampleParentId = parentId;

            copy.Save(txOrNull);

            //Deep Copy - Parent Entities: Cloned parents also clone their child collections
            //this.Children.Clone(target, txOrNull, copy.TagId);

            return copy;
        }
        #endregion

        #region ToXml
        protected override void ToXml_Custom(System.Xml.XmlWriter w)
        {
            //Store(w, "Example", this.Example)
        }
        #endregion
    }
}